\documentclass{article}
\pagestyle{plain}

\usepackage{graphicx}
% \usepackage{amssymbol}
\usepackage[latin1]{inputenc}
\usepackage{amssymb}
\usepackage{color}
\usepackage{parskip}

\usepackage{gnuplot-lua-tikz}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage{adjustbox}
\usepackage{pbox}

\begin{document}

\setcounter{page}{1}
\begin{center}
{\Large \bf Rivers and the Stock Market}
\end{center}

\begin{center}
Renato Fernandes $^{1}$, Ricardo Cruz $^{1}$, Alberto Pinto $^1$ \\
$^1$ Faculdade Ci\^{e}ncias, Universidade do Porto \\
\end{center}
 
\begin{abstract}
The BHP distribution has been found to be useful in modeling various natural and human phenomena. We will fit flows of the Paiva river for the BHP, and apply the same tools for stock market flows. The river flow from the first and latter decades is examined to test against climate change.
 \end{abstract}

\section{Introduction}

The Paiva River is a natural river from Portugal on the northern part of the territory untouched by man, i. e., there are no dams or dikes that influences the river flow, so all it's movement is as it was always meant by nature. This kind of flow is supposed to be unpredictable because it is all dependant on the natural effects like rain, heat and so on... And as we know, even we trying to predict the weather, from time to time, we make our predictions wrong. 

One other interesting fact about the Paiva River is that its data distribution for the runoff values is very similar to the ones of the stock market prices and by developing tools to study this river, we can also study and compare the data from stock markets.

Here we reproduce programs that were lost and create some new ones to do this study, it was already found that the fluctuations of the runoffs values, from days with no rain, for the Paiva River followed the BHP distribution. \cite{Goncalves:09} We will study here a more comprehensive set of situations allowing the mixing of days with and without rain. We will as well try to fit all this situations to the BHP distribution.

\begin{figure}[htp]
\centering
\includegraphics[scale=1]{bhp.png}
\caption{BHP density function. We have used a BHP distribution table calculated by Gon\c{c}alves. \cite{Goncalves:09}}
\label{}
\end{figure}

\begin{figure}[htp]
\centering
\input{diagram-m1.tex}
\caption{Markov chain with memory .}
\end{figure}

\section{Markov chains}

Given the flow data $\vec{X}$, if we look at the previous days with a window $m$, we shall have $\vec{A}(t) = \{ X_{t-m}, ..., X_{t-1} \}$ and $\vec{B}(t) = \{ X_{t-m+1}, ..., X_{t} \}$. From there we calculate the difference vector $\vec{R}(t) = \vec{B}(t) - \vec{A}(t)$. If we call 0 to decreases of flow, and 1 to increases, we may classify those several flow windows as 010 for a decrease then an increase followed by a decrease the next day (given $m=3$). We thereafter refer to those states using the decimal representation of those binary codifications. The flow is calculated using the formula $rd(t) = \frac{X_{t+1}-X_t}{X_t}$. We are thus considering only the last two days, independent of the window length. [depois de escrever isto .. acho q estamos a fazer isto mal .. o goncalves usa a media parece .. o problema podem ser os sinais ..]

\begin{figure}[htp]
\centering
\begin{tikzpicture}
\draw (0,0) rectangle (1,.6); \draw (.5,.3) node {1.72};
\draw (1,0) rectangle (2,.6); \draw (1+.5,.3) node {1.69};
\draw (2,0) rectangle (3,.6); \draw (2+.5,.3) node {1.73};
\draw (3,0) rectangle (4,.6); \draw (3+.5,.3) node {1.66};
\draw (4+.5,.3) node {...};
\draw[|-|] (0,-.3) -- (3,-.3); \draw (3/2,-.3) node[below] {$m$};
\draw[|-|] (1,-.7) -- (4,-.7); \draw (1+3/2,-.7) node[below] {$m$};
\end{tikzpicture}
\caption{Showing the split of data ($m=3$).}
\end{figure}

We first define a flow window by $FW$ which is a binary vector of size $m\in\mathbb{N}$ and an $\alpha \in [0,1]$.

Then the data to be used is defined by

$T=\{t:\vec{R}(t)=FW\}$

We also define $\mu_\alpha$, $\sigma_\alpha$ as the mean and standard deviation of $rd(t)^\alpha$ for $t\in T$ and the $\alpha$ fluctuations by

$r_\alpha (t)=\frac{rd(t)^\alpha - \mu_\alpha}{\sigma_\alpha}$

The $\alpha$'s used in this work are the $\alpha$'s that best fit $r_\alpha(t)$ to the BHP distribution for each $FW$. We find these $\alpha$'s using the Kolmogorov-Smirnov statistical test.

We study markov chains from the river flows, as they increase or decrease. We try multiple combinaions of increases or decreases, with multiple size memory, for memory 4 we may have increase in first day, increase in the second, decrease after and increase in the fourth  From those we choose the ones with more significative value, this means that we show the ones that present a more 

(fazer estudo para os outros dados tambem .. vai eventualmente para a sua propria seccao. dps dividimos isto em seccoes qdo ficar maior ..)

\begin{figure}[htp]
\centering
\input{table-m0-paiva.tex}
\caption{Markov chain with memory 0.}
\end{figure}

\begin{figure}[htp]
\centering
\input{table-m1-paiva.tex}
\caption{Markov chain with memory 1.}
\end{figure}

\begin{figure}[htp]
\centering
\input{table-m2-paiva.tex}
\caption{Markov chain with memory 2.}
\end{figure}

\begin{figure}[htp]
\centering
\input{table-m3-paiva.tex}
\caption{Markov chain with memory 3.}
\end{figure}

\section{Alpha study}

$\alpha(day)$ for 12 months, in which each day is calculated as the alpha mean, within a radius of 30 days, through the years. The confidence interval is calculated assuming the alphas follow a Normal.

(ver isto melhor .. nomeadamente a parte do intervalo de confianca)

(o papel do alpha ja devera ter sido explicado anteriormente.)

\begin{figure}[htp]
\centering
\includegraphics[scale=0.90]{alphaday.png}
\caption{$\alpha(day)$}
\label{}
\end{figure}

\begin{figure}[htp]
\centering
\includegraphics[scale=0.65]{alphamonth.png}
\caption{$\alpha(month)$}
\label{}
\end{figure}

\section{Climate change test}

Testing if flow samples through the years follow the same distribution to test for climate change. From this analysis, no climate change was identified. Or rather, this river does not seem to suffer effects from climate change since not many great variations were found.

(ver em no relatorio do IPCC desde quando o aquecimento e' considerado como significativo. referir isso aqui.)

(falta fazer os testes binomiais)

\begin{figure}[htp]
\centering
\includegraphics[scale=1.00]{climatetest.png}
\caption{Kolmogorov-Smirnov test, comparing 2 pairs of samples through the years (in a 4, 10 and 20 year diameter, around each  year). In black, $pvalues < 0.01$. In gray, $pvalues < 0.05$. (gerar em format latex. virar ao contrario.)}
\label{}
\end{figure}

\include{table-final1-paiva}

\include{table-final2-paiva}

\begin{figure}[htp]
\centering
\includegraphics[scale=0.70]{markovbinomtest.png}
\caption{Binomial test, comparing 2 markov chains through the years (in a 4, 10 and 20 year diameter, around each  year). In black, the distributions have the same probability of occuring with a significance lvl of $95\%$. In white, the distributions probabilities don't match.}
\label{}
\end{figure}

% -- Bibliography --

\begin{thebibliography}{1}

\bibitem{Bramwell:98}
 S. T. Bramwell, P. C. W. Holdsworth, J. F. Pinton.
 Universality of rare fluctuations in turbulence and critical phenomena.
 In
 {\em Nature} Vol 396, 552-554, 1998.

\bibitem{Goncalves:09}
 R. Gon\c{c}alves, H. Ferreira, A. Pinto, N. Stollenwerk.
  Universality in nonlinear prediction of complex systems.
 In
 {\em Journal of Difference Equations and Applications} 15:11-12, 1067-1076, 2009.

\bibitem{ref} 
 R. Gon\c{c}alves, H. Ferreira, A. Pinto.
  Universality in the stock exchange
  market
 In
  {\em Journal of Difference Equations and Applications} 17:7, 1049-1063, 2011.

\end{thebibliography}


\end{document}
